package com.gym.controller.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gym.controller.form.admin.UpdateCourseForm;
import com.gym.model.dao.ConnessioneException;
import com.gym.model.dao.DAOCourse;
import com.gym.model.dao.DatoNonTrovatoException;
import com.gym.model.dao.MetodoDaoException;
import com.gym.model.entity.Course;

public class UpdateCourse extends Action{
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		UpdateCourseForm f = (UpdateCourseForm) form;

		int id = Integer.parseInt(request.getParameter("id"));
		
		String activity=f.getActivity();
		String note=f.getNote();
		Integer idtrainer=Integer.parseInt(f.getIdtrainer()) ;
		short maxcustomers=Short.parseShort(f.getMaxcustomers());
		short lifespan=Short.parseShort(f.getLifespan());
		double price=Double.parseDouble(f.getPrice());
		
		Course course = new Course(id, activity, note, idtrainer, maxcustomers, lifespan, price);
		
		try {
			DAOCourse dao = new DAOCourse();
			dao.update(course);
			
			request.setAttribute("feedback", "OK, modificato il corso "+activity);
			return mapping.findForward("success");
			
		} catch (ConnessioneException | MetodoDaoException | DatoNonTrovatoException e) {
			request.setAttribute("error", e.getMessage());
			return mapping.findForward("failure"); 
		}
		
	}

}
